using System;
using System.Text.RegularExpressions;

namespace ITMSClient
{
    /// <summary>
    /// Regular expression parsers for iTunes movie XML pages
    /// </summary>
    internal sealed class RegexParsers
    {
        /// <summary>
        /// Used to find the link to the next index page from the current index page
        /// Returns the url and page number
        /// </summary>
        internal static Regex indexNextPageRegex = new Regex(
            @"\<GotoURL url=""(?<url>\w*_(?<pagenum>\d{1,3})\.xml)""\>\s*\<TextView[^/]*?\<B\>More",
            RegexOptions.Singleline
            | RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );
        internal static Regex indexBackPageRegex = new Regex(
            @"\<GotoURL url=""(?<url>\w*_(?<pagenum>\d{1,3})\.xml)""\>\s*\<TextView[^/]*?\<B\>Back",
            RegexOptions.Singleline
            | RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );

        /// <summary>
        /// Used to pull the title, studio and url link for each movie trailer on the index page
        /// </summary>
        internal static Regex indexDetailsRegex = new Regex(
            //"<GotoURL[^>]+url=\"(?<url>/moviesxml/[^\"]+?/index.xml)\">\\s*<B>(?<title>[^<]+)</B>.+?"
            //+ "<GotoURL[^>]+>\\s*(?<studio>.+?)\\s*</GotoURL>",
            //"<GotoURL[^>]+url=\"(?<url>/moviesxml/[^\"]+?/index.xml)\">\\s*<[^>]+><B>(?<title>[^<]+)</B>[^>]+>(?>\\s+[^>]+>)+\\s+<GotoURL[^>]+>\\s*<[^>]+>(?<studio>.+?)\\s*</SetFontStyle>",
            //"<GotoURL[^>]+url=\"(?<url>/moviesxml/[^\"]+?/index.xml)\">\\s*<[^>]+><B>(?<title>[^<]+)</B>[^>]+>.+?<GotoURL[^>]+>\\s*<[^>]+>(?<studio>.+?)\\s*</GotoURL>",
            @"<PictureView[^.]+ url=""(?<poster>[^""]*.jpg)"".+?<GotoURL[^>]+url=""(?<url>/moviesxml/[^""]+?/index.xml)"">\s*<[^>]+><B>(?<title>[^<]+)</B>[^>]+>.+?<GotoURL[^>]+>\s*<[^>]+>(?<studio>.+?)\s*</GotoURL>",
            RegexOptions.Singleline
            | RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );

        internal static Regex indexDetailsRegex2 = new Regex(
      "<GotoURL url=\"(?<url>[^\"]*)\".+?\\s*<PictureView.+?url=\"(" +
      "?<poster>[^\"]+)\".+?[^>]+>.+?[^>]+>.+?[^>]+>.+?[^>]+>.+?[^>" +
      "]+>.+?\\s*<b>(?<title>[^<]+)</b>.+?[^>]+>.+?[^>]+>.+?[^>]+>." +
      "+?[^>]+>.+?\\s*(?<studio>.+?)\\s*</",
    RegexOptions.Singleline
            | RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
    );
        internal static Regex indexBoxOfficeRegex = new Regex(
              "<GotoURL target=\"[^\"]*\" url=\"(?<url>[^\"]*)\".+?\\s*[^>]" +
      "+>.+?[^>]+>.+?[^>]+>.+?[^>]+>.+?[^>]+>.+?[^>]+>.+?[^>]+>\\s*" +
      "<b>(?<title>[^<]+)</b>(?<poster>)(?<studio>)",
RegexOptions.Singleline
            | RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );

        /// <summary>
        /// Used to capture all the movie details from the movie details index.xml page
        /// </summary>
        internal static Regex moviedetailsRegex1 = new Regex(
            @"<PictureView.+?url=""(?<poster>[^""]+)"".+?"
            + @"<TextView[^>]*>\s*[^>]+>\s*<b>(?<title>[^<]+?)</b>.+?"
            + @"<TextView[^>]*>\s*[^>]+>\s*(?<copyright>[^<]+?)\s*<.+?"
            + @"<TextView[^>]*>\s*[^>]+>\s*(?<overview>[^<]*?)\s*<",
            RegexOptions.Singleline
            | RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );

        internal static Regex moviedetailsRegex2 = new Regex(
            //@"Release:\s*(?<release>[^<]+?)\s*<.+?"
            //+ @"<TextView[^>]*>(\s*<GotoURL[^>]*>(?<genres>[^<]+)</GotoURL>,?){0,}\s*</TextView>.+?"
            //+ @"Rating:\s*(?<rating>.+?)\s*<.+?"
            //+ @"(<TextView[^>]*>\s*(?<cast>[^<]*?)\s*</TextView>.+?){0,}</VBoxView>.+?"
            // @"(<GotoURL[^>]+url=""(?<urls>/moviesxml/[^>]+?/index.xml)"">\s*<TextView.+?){0,}</Document>",
            @"<b>In\sTheaters:</b>(?<release>[^<]*)\s*<.+?Genre:[^>]*>(?<genre>[^<]*)",
            RegexOptions.Singleline
            | RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );
		internal static Regex movieVideoUrlRegex = new Regex(
        	@"(<GotoURL[^>]+url=""(?<urls>/moviesxml/[^>]+?/index.xml)"">\s*<TextView.+?){0,}</Document>",
        	 RegexOptions.Singleline
            | RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );
        internal static Regex dateRegex = new Regex(
            @"(?<month>\w+) (?<day>\d+).+?(?<year>(19|20)\d\d)",
            RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );

        /// <summary>
        /// Used to find the different sizes of Quicktime trailer movies available from 
        /// the trailer size selection page
        /// Returns the url and the size of the movie
        /// </summary>
        internal static Regex streamRegex = new Regex(
            //@"<GotoURL.+?url=""(?<url>.*?(?<size>(small|medium|large|fullscreen))\.xml)""",
            @"<key>previewURL</key><string>(?<url>[^<]*(?<size>h[\d]*p*)[w]*\.mov)",
            RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );

        //Used to find the multiple trailer urls		
        internal static Regex trailerLinkRegex = new Regex(
            "<HBoxView[^>]*>\\s*<GotoURL\\starget=\"main\"\\surl=\"(?<url>[^\"]*)[^=]*=[^=]*=\"(?<name>[^\"]*)",
            RegexOptions.Singleline
            | RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );


        /// <summary>
        /// Used to pull the type and length of the Quicktime trailer movies available from 
        /// the trailer size selection page
        /// </summary>
        internal static Regex streamDetailsRegex = new Regex(
            @"<b>(?<type>.+?)\s\((?<minutes>\d{0,2}):(?<seconds>\d{2})\)?</b>",
            RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );

        /// <summary>
        /// Used to find the actual Quicktime movie on the final movie playback page
        /// Returns the url of the .mov file
        /// </summary>
        internal static Regex movRegex = new Regex(
            @"\<MovieView.+?url=""(?<url>.+?\.mov)""",
            RegexOptions.ExplicitCapture
            | RegexOptions.Compiled
            );

    }
}
